<!--
	作者：ZhangXin
	时间：2020-07-24
	描述：博客分类管理页面
-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="admin/fragments::head(~{::title})">
	<title>博客分类管理</title>
</head>

	<body>
	<!--导航-->
	<nav th:replace="admin/fragments::menu(2)"></nav>

		<!--中间内容-->
		<div class="m-container-mini m-padded-tb-big ">
			<div class="ui container">
				<div class="ui success message" th:unless="${#strings.isEmpty(successMessage)}">
					<i class="close icon"></i>
					<div class="" th:text="${successMessage}">提示：操作成功！</div>
				</div>
				<div class="ui negative message" th:unless="${#strings.isEmpty(errorMessage)}">
					<i class="close icon"></i>
					<div class="" th:text="${errorMessage}">提示：操作失败！</div>
				</div>
				<table class="ui compact teal table" >
					<thead>
					<tr>
						<th colspan="6"><h2 class="ui teal header" style="text-align: center">博客分类列表</h2></th>
					</tr>
						<tr align="center">
							<th></th>
							<th>名称</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<tr align="center" th:each="type,iterStat:${page.content}">
							<td th:text="${iterStat.count}">1</td>
							<td th:text="${type.name}">我的故事</td>
							<td>
								<button type="button"  href="#" class="ui updateBtn mini teal basic button" th:data-id= "${type.id}"  onclick="editType(this.getAttribute('data-id'))">编辑</button>
								<a href="#" onclick="return confirm('确定要删除该分类吗？三思啊! 删了可就没了！')" th:href="@{/admin/types/{id}/delete(id=${type.id})}" class="ui mini red basic button">删除</a>
							</td>
						</tr>
					</tbody>
					<tfoot>
						<tr>
							<th colspan="6">
								<div class="ui inverted divided stackable grid">
									<div class="three wide column" align="center">
										<a class="ui mini teal basic button item" th:href="@{/admin/types(page=${page.number}-1)}" th:unless="${page.first}">上一页</a>
									</div>

									<div class="ten wide column" align="center">
										<p>第 <span>[[${page.number}+1]]</span> 页，共 <span>[[${page.totalPages}]]</span> 页，有 <span>[[${page.totalElements}]]</span> 个分类</p>
									</div>

									<div class="three wide column" align="center">
										<a class="ui mini teal basic button item" th:href="@{/admin/types(page=${page.number}+1)}" th:unless="${page.last}">下一页</a>
									</div>
								</div>
								<div align="center">
									<a href="#">
										<button id="addBtn" type="button" class="ui teal button m-mobile-wide m-margin-top"><i class="pencil icon"></i>新增</button>
									</a>
								</div>
							</th>
						</tr>
					</tfoot>
				</table>
			</div>
			<!--添加分类-->
			<div class="ui container">
				<div class="ui small add modal" style="height: 400px;">
					<h2 class="ui teal header" style="text-align: center;">添加分类</h2>
					<form action="#" th:action="@{/admin/types}"  method="post" class="ui form">
						<input type="hidden" name="id">
						<div class=" field  m-padded-tb-big m-padded" >
							<div class="ui left labeled input">
								<label class="ui teal basic label">名称</label>
								<input type="text" name="name" placeholder="分类名称" >
							</div>
						</div>
						<div class="ui error message" ></div>
						<div class="ui center aligned container actions">
							<button class="ui teal submit button">提交</button>
							<button type="button" class="ui cancel red button">取消</button>
						</div>
					</form>
				</div>
			</div>

			<!--修改分类-->
			<div class="ui container">
				<div class="ui small update modal" style="height: 400px;">
					<h2 class="ui teal header" style="text-align: center;">修改分类</h2>
					<form action="#" th:action="@{/admin/types}"  method="post" class="ui form">
						<input type="hidden" name="_method" value="put" />
						<input type="hidden" name="id" id="type_id">
						<div class=" field  m-padded-tb-big m-padded" >
							<div class="ui left labeled input">
								<label class="ui teal basic label">名称</label>
								<input type="text" id="type_name" name="name" placeholder="分类名称" >
							</div>
						</div>
						<div class="ui error message" ></div>
						<div class="ui center aligned container actions">
							<button class="ui teal submit button">提交</button>
							<button type="button" class="ui cancel red button">取消</button>
						</div>
					</form>
				</div>
			</div>

		</div>

		<br />
		<br>
		<br>
		<!--底部内容-->
		<footer th:replace="admin/fragments::footer"></footer>

	    <script th:replace="admin/fragments::script"></script>

		<script>
			$('.menu.toggle').click(function() {
				$('.m-item').toggleClass('m-mobile-hide')
			})



			$('.menu.toggle').click(function() {
				$('.m-item').toggleClass('m-mobile-show');

			});

			$('.ui.dropdown').dropdown({
				on: 'hover'
			});

			$(function() {
				$("#addBtn").on("click", function() {
					$(".ui.add.modal").modal("show");
				})
			});

			$(function() {
				$(".updateBtn").on("click", function() {
					$(".ui.update.modal").modal("show");
				})
			});

			$('.ui.form').form({
				fields: {
					title: {
						identifier: 'name',
						rules: [{
							type: 'empty',
							prompt: '请输入分类名称'
						}]
					}
				}
			});

			//消息提示关闭初始化
			$('.message .close')
				.on('click', function() {
					$(this)
						.closest('.message')
						.transition('fade');
				});

			//获取修改分类的名称
			function editType(id) {
				$.ajax({
					type:"post",
					url:"/admin/types/input",
					data:{"id":id},
					//dataType:"json",
					success:function(data) {
						var arr= new Array(); //定义一数组
						arr =data.split("|");
						//alert(arr[0]);
						$("#type_id").val(arr[0]);
						$("#type_name").val(arr[1]);

					}
				});
			}
		</script>
	</body>

</html>